package com.flyoil.petromp.ui.activity.activity_me;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.ConHander;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.ui.activity.activity_login.LoginActivity;
import com.flyoil.petromp.utils.ApplicationHelp;
import com.flyoil.petromp.utils.Dialog.DialogHintUtil;
import com.flyoil.petromp.utils.Dialog.Dialog_util;
import com.flyoil.petromp.utils.HintConstants;
import com.flyoil.petromp.utils.MoccaPreferences;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;

/**
 * 设置界面
 */
public class SetupActivity extends BaseActivity {
    private LinearLayout line_exit;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_setup;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setTextTitleName("设置");
        httpModel = new HttpModel();
        line_exit = $(R.id.line_setup_exit);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        line_exit.setOnClickListener(onClickListener);
        setBackOnclickListner(mContext);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.line_setup_exit:
                    show_exit_app_dialog();
                    break;
            }
        }
    };

    private void show_exit_app_dialog() {
        DialogHintUtil.show_hint_message_diolog(mContext);
        DialogHintUtil.set_dialog_content("退出后不会删除您的任何历史数据");
        DialogHintUtil.set_dialog_confirm("退出");
        DialogHintUtil.set_confirm_onclicklistener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logout();
            }
        });
    }

    /**
     * 退出登录
     */
    private void logout() {
        DialogHintUtil.dismiss_dialog();
        Dialog_util.start_NetworkRequests_diolog(mContext);
        Map<String, Object> map = new HashMap<>();
        httpModel.logout_server(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onFailure(String msg) {
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogHintUtil.dismiss_dialog();
                MoccaPreferences.sessionId.put("");
                MoccaPreferences.login_pwd.put("");
                MoccaPreferences.user_login.put(false);
                ApplicationHelp.setHandler(ConHander.HOME_HANDER);
                startActivity(new Intent(mContext, LoginActivity.class));
                finish();
            }
        });
    }


}
